/**
 * Android
 * @returns {*}
 */
//android获取所有
function androidpullAllMsg() {
    var requestJson = '{"fields":["accountId","token","channelId"]}';
    var responseJson = window.native.pull(requestJson);
    console.log("获取渠道信息" + responseJson);
    var info = $.parseJSON(responseJson);
    if (info.resultCode == "0") {
        return info;
    } else {
        console.log("android获取参数失败 accountId、token、channelId");
    }

}
    function androidpullOrderNoAccountidMsg() {
        var requestJson = '{"fields":["accountId","orderNo"]}';
        var responseJson = window.native.pull(requestJson);
        console.log("获取帐户编号、订单信息" + responseJson);
        var info = $.parseJSON(responseJson);
        if (info.resultCode == "0") {
            return info;
        } else {
            console.log("android获取参数失败 accountId、orderNo");
        }
    }

//android获取所有
    function androidpullAccountidMsg() {
        var requestJson = '{"fields":["accountId"]}';
        var responseJson = window.native.pull(requestJson);
        console.log("获取渠道信息" + responseJson);
        var info = $.parseJSON(responseJson);
        if (info.resultCode == "0") {
            return info;
        } else {
            console.log("android获取参数失败 accountId");
        }
    }

//android获取渠道编号
    function androidpullchannelIdMsg() {
        var requestJson = '{"fields":["channelId"]}';
        var responseJson = window.native.pull(requestJson);
        console.log("获取渠道信息" + responseJson);
        var info = $.parseJSON(responseJson);
        //alert("拿到渠道信息"+info);
        console.log("从app拿取到json对象", info, info.resultCode, info.resultMsg.channelId);
        if (info.resultCode == "0") {
            return info;
        } else {
            console.log("android登录获取渠道编号失败");
        }
    }

//android存储数据
    function androidpushMsg(accountid, token, refreshtoken,name) {
        var jsonData = "{\"accountId\":\"" + accountid + "\",\"token\":\"" + token + "\",\"refreshToken\":\"" + refreshtoken + "\",\"name\":\"" + name + "\"}";
        window.native.push(jsonData);
        console.log("android登录后，存储数据到app成功")
    }


    /**
     * IOS
     * @returns {sendjson.channelId|*|result.channelId}
     */

    function iospullAllMsg() {
        var requestJson = '{"fields":["accountId","token","channelId"]}';
        var responseJson = iosPull(requestJson);
        console.log("获取渠道信息" + responseJson);
        var info = $.parseJSON(responseJson);
        if (info.resultCode == "0") {
            return info;
        } else {
            console.log("android获取参数失败 accountId、token、channelId");
        }
    }

    function iospullAccountidOrdernoMsg() {
        var requestJson = '{"fields":["accountId","orderNo"]}';
        var responseJson = iosPull(requestJson);
        console.log("获取帐户编号、订单信息" + responseJson);
        var info = $.parseJSON(responseJson);
        if (info.resultCode == "0") {
            return info;
        } else {
            console.log("android获取参数失败 accountId、orderNo");
        }
    }

//ios获取所有
    function iospullAccountidMsg() {
        var requestJson = '{"fields":["accountId"]}';
        var responseJson = iosPull(requestJson);
        console.log("获取渠道信息" + responseJson);
        var info = $.parseJSON(responseJson);
        if (info.resultCode == "0") {
            return info;
        } else {
            console.log("android获取参数失败 accountId");
        }
    }

//ios获取渠道编号
    function iospullchannelIdMsg() {
        var requestJson = '{"fields":["channelId"]}';
        var responseJson = iosPull(requestJson);
        console.log("获取渠道信息" + responseJson);
        var info = $.parseJSON(responseJson);
        if (info.resultCode == "0") {
            return info;
        } else {
            console.log("ios登录获取渠道编号失败");
        }
    }

//ios存储数据
    function iospushMsg(accountid, token, refreshtoken,name) {
        var jsonData = "{\"accountId\":\"" + accountid + "\",\"token\":\"" + token + "\",\"refreshToken\":\"" + refreshtoken + "\",\"name\":\"" + name + "\"}";
        iosPush(jsonData);
        console.log("ios登录后，存储数据到app成功")
    }


    /**
     * 判断终端是Android 或者IOS
     * @returns {boolean}
     */
//检查当前应用是否为android
    function checkTerminalIsAndroid() {
        var u = navigator.userAgent;
        return u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
    }

//检查当前环境是否为android
    function checkTerminalIsiOS() {
        var u = navigator.userAgent;
        return !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
    }


    /**
     * 返回操作系统
     */
//返回ios系统
    function returnToIos() {
        iosPush("");
        console.log("返回ios app");
    }


//android存储数据
    function returnToAndroid() {
        window.native.push("");
        console.log("返回android app")
    }


    /**
     * 根据终端判断获取哪种数据
     */

//获取所有数据
    function pullAllMsg() {
        if (checkTerminalIsAndroid()) {
            return androidpullAllMsg();
        }
        if (checkTerminalIsiOS) {
            return iospullAllMsg();
        }
    }

    //获取帐户编号 订单号数据
    function pullAccountidOrdernoMsg() {
        if (checkTerminalIsAndroid()) {
            return androidpullOrderNoAccountidMsg();
        }
        if (checkTerminalIsiOS) {
            return iospullAccountidOrdernoMsg();
        }
    }

//获取帐户编号
    function pullAccountidMsg() {
        if (checkTerminalIsAndroid()) {
            return androidpullAccountidMsg();
        }
        if (checkTerminalIsiOS) {
            return iospullAccountidMsg();
        }
    }

//获取渠道编号
    function pullChannelidMsg() {
        if (checkTerminalIsAndroid()) {
            return androidpullchannelIdMsg();
        }
        if (checkTerminalIsiOS) {
            return iospullchannelIdMsg();
        }
    }

//完成业务自动返回app
    function returnToApp() {
        if (checkTerminalIsAndroid()) {
            returnToAndroid();
        }
        if (checkTerminalIsiOS()) {
            returnToIos();
        }
    }

//登录完成后，存储数据到APP
    function pushAllMsgToApp(accountId, token, refreshToken,name) {
        if (checkTerminalIsAndroid()) {
            androidpushMsg(accountId, token, refreshToken,name);
        }
        if (checkTerminalIsiOS()) {
            iospushMsg(accountId, token, refreshToken,name);
        }

    }

